//
//  CommentViewController.m
//  代号：47
//
//  Created by 杨佩璋 on 15/7/3.
//  Copyright (c) 2015年 杨佩璋. All rights reserved.
//

#import "CommentViewController.h"
#import "AFNetworking.h"
#import "CommentCell.h"
#import "ChartsViewController.h"
#import "CatDetailViewController.h"



@interface CommentViewController () <UITableViewDelegate,UITableViewDataSource>
{
    MBProgressHUD *hud;
}
@property(nonatomic,retain)NSMutableArray *modelArr;
@property (nonatomic,retain)AFHTTPRequestOperation *operation; // 操作。
@property (nonatomic,retain) NSOperationQueue *queue;  // 操作队列
@property (nonatomic,retain)UITableView *mainTableView; // 主tableView
@end

@implementation CommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.offset = @0;

    self.view.backgroundColor = [UIColor whiteColor];
    self.modelArr = [NSMutableArray array];
    [self createTableView];
    [self createdownLoadProgress];
    [self addRefresh];
    [self requestData];
    
    // Do any additional setup after loading the view.
}


// 添加刷新
- (void)addRefresh{
    //下拉刷新
    [self.mainTableView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderDefault refreshingBlock:^{
        
        [self.modelArr removeAllObjects];
        self.offset = @0;
        [self requestNewData];
    }];
    
    
    // 上拉加载
    [self.mainTableView addRefreshWithRefreshViewType:LORefreshViewTypeFooterDefault refreshingBlock:^{
        NSInteger number = [self.offset integerValue];
        number += 15;
        self.offset = [NSNumber numberWithInteger:number];
        [self requestNewData];
    }];
    
    
}



// 添加进度条
// 加载进度条
- (void)createdownLoadProgress{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    hud = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:hud];
    hud.labelText = @"努力加载中..";
    //设置模式为环状进度提示框
    [hud show:YES];
    [hud release];
}




- (void)createTableView
{
    self.mainTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreen_width, kScreen_height) style:UITableViewStylePlain];
    [_mainTableView registerClass:[CommentCell class] forCellReuseIdentifier:@"commentcell"];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    [_mainTableView release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.modelArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommentModel *model;
    CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"commentcell" forIndexPath:indexPath];
    if (self.modelArr.count>0) {
        model = [self.modelArr objectAtIndex:indexPath.row];
        cell.model = model;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 115;
    
}


// AFN 数据请求
- (void)requestData
{
    NSURL *url = [NSURL URLWithString:@"http://api.maoyan.com/sns/subject/7/targets.json?client=iphone&ci=10&limit=15&msid=4B397E69-F5E0-46F3-B54F-723B6941E1332015-07-01-16-46643&offset=0&utm_campaign=AmovieBmovie&utm_content=B823FD0D620E08201195D79B8988CF53033C9107086C772974130EB7748F8C14&utm_medium=iphone&utm_source=AppStore&utm_term=5.7&uuid=B823FD0D620E08201195D79B8988CF53033C9107086C772974130EB7748F8C14&version_name=5.7&movieBundleVersion=100"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    self.operation = operation;
    [operation release];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *html = operation.responseString;
        NSData* data=[html dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        NSArray *arr = dic[@"data"];
        for (NSDictionary *dic in arr) {
            CommentModel *model = [[CommentModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.modelArr addObject:model];
            [model release];
        }
        
        
        [hud removeFromSuperview];
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        [self.view addSubview:_mainTableView];
        [self.mainTableView reloadData];
    } failure:nil];
    
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperation:operation];
    self.queue = queue;
    [queue release];
}

// 新requestData

- (void)requestNewData
{
    NSString *str = [NSString stringWithFormat:@"http://api.maoyan.com/sns/subject/7/targets.json?client=iphone&ci=10&limit=15&msid=4B397E69-F5E0-46F3-B54F-723B6941E1332015-07-01-16-46643&offset=%@&utm_campaign=AmovieBmovie&utm_content=B823FD0D620E08201195D79B8988CF53033C9107086C772974130EB7748F8C14&utm_medium=iphone&utm_source=AppStore&utm_term=5.7&uuid=B823FD0D620E08201195D79B8988CF53033C9107086C772974130EB7748F8C14&version_name=5.7&movieBundleVersion=100",self.offset];
    
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    self.operation = operation;
    [operation release];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *html = operation.responseString;
        NSData* data=[html dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        NSArray *arr = dic[@"data"];
        for (NSDictionary *dic in arr) {
            CommentModel *model = [[CommentModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.modelArr addObject:model];
            [model release];
        }
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        [self.mainTableView reloadData];
        [self.mainTableView.defaultFooter endRefreshing];
        [self.mainTableView.defaultHeader endRefreshing];
    } failure:nil];
    
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperation:operation];
    self.queue = queue;
    [queue release];
}





-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CommentModel *model = [self.modelArr objectAtIndex:indexPath.row];
    
    CatDetailViewController *catDe = [[CatDetailViewController alloc] init];
    catDe.number = model.ID;
    [self presentViewController:catDe animated:YES completion:^{
        
    }];
    [catDe release];
    
}




-(void)dealloc
{
    [_offset release];
    [_mainTableView release];
    [_operation release];
    [_queue release];
    [_modelArr release];
    [super dealloc];
}


@end
